#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
【需求背景】
在产品端需要上线联系方式来源的快照，现需要对库内的数据情况进行盘点

【需求详情】
库内有接近3.2亿的联系方式，按来源展开，应该会有接近20亿的来源，我需要知道有快照的比例是多少
'''

import sys

sys.path.append('/mnt/moonbrook/Moonbrook')
from aes import contact_aes, name_aes
import os
import re
import json
import time
import requests
from copy import deepcopy
from datetime import datetime, timedelta
from apscheduler.schedulers.blocking import BlockingScheduler

from base import ExportAndTjBase
from models import MongoQueryConfig, RobotConfig
from utils import Utils
from robot import DingtalkRobot

reload(sys)
sys.setdefaultencoding('utf-8')


class ExportAndTj(ExportAndTjBase):
    week_min, week_max = Utils.get_date_range(ndays=7)


    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        self.silk_icp_col = self.silk_db.get_collection('icp')
        # todo other col



    def handle_one_mongo_data(self, record_dic, save_result_dic=None):
        # 解密，提取字段数据
        create_time = record_dic.get('create_time','')
        if isinstance(create_time,datetime):
            t = create_time.strftime("%Y-%m-%d")
            if t not in save_result_dic:
                save_result_dic[t] = 0
            save_result_dic[t] += 1

        return save_result_dic

    def run(self):
        # 需要统计的维度
        save_result_dic = {
        }
        config = {
            MongoQueryConfig.col_name: 'icp',
            MongoQueryConfig.filter: {
                # "nameId": {"$exists": True},
                # 'icpList.domain': {"$exists": True},
                # "icp_failed_no": {"$lt": 3},
                "create_time": {"$gte": datetime(year=2021,month=7,day=1)},
            },
            MongoQueryConfig.projection: ["create_time"],
        }
        result_dic = self.get_data_from_mongo(config, save_result_dic)


def run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='20', minute='52', id='dd_run')
    scheduler.start()


if __name__ == '__main__':
    file_name = '每日新增'
    run(file_name)
    # scheduler_run(file_name)
